<?php
/**
 * This page handles the 'editView'
 * editView is the editing page for a project. Found at /edit/([a-zA-Z0-9_]+)
 * 
 * This page can only be accessed using mod_rewrite.
 */

    $fileprefix = '../';
    if(isset($_ECLIPSEMDE['ROOT_WD']) === true)
	$fileprefix = $_ECLIPSEMDE['ROOT_WD'];
    require_once($fileprefix.'SiteIncludes.GLOBAL.php');
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	$gVAR['e'] = '404';
	require_once('error_handler.php');
	die();
    }
    
    
    
    // Ensure all variables needed are set
    if(isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false && isset($gVAR['alias']) !== false) {
	try {
	    $Project =& $ProjectStash->get($gVAR['alias']);
	    // Tool Name in the JS
	    $TemplateEngine->add_preloadScript('<script type="text/javascript">var tool_name=\''.$Project->getAlias().'\';</script>');
	    
	    if($Project->userCanEdit($_LOGGEDINUSER->get_userid()) === true) {
		$TemplateEngine->setHasSidebar(false);
		$TemplateEngine->setCentralContentFullWidth(true);
		
		$editer_line = <<<EOEL
		
		    <div class="row-fluid">
			<div class="span12" style="text-align: right;">
			    <span style="border-bottom: 1px dotted #989898;"><span style="font-weight: bold;">Changelog:</span> {$Project->getName()} </span><img src="/img/set.gif" style="width: 16px; height: 16px; border-style: 0px solid none;" alt="Editing" /> (<span style="font-style: italic;"><a href="javascript: history.go(-1);">Back</a></span>)</span>
			</div>
		    </div>
EOEL;

		$table_head = <<<EOHEAD
		    <table style="width: 55%; margin-left: auto; margin-right: auto;" border>
EOHEAD;
		$table_body = '';
		$query = $sql_conx->query("SELECT `uid_editor`, `time`, `type`, `info1`, `info2` FROM `tool_logger` WHERE `pid`='{$Project->getID()}' ORDER BY `time` DESC LIMIT 35;");
		$table_body .= <<<EOBODY
			    <tr>
				<td style="width: 25%; font-weight: bold; text-align: center;">Time</td>
				<td style="width: 25%; font-weight: bold; text-align: center;">Action</td>
				<td style="width: 25%; font-weight: bold; text-align: center;">User</td>
				<td style="width: 25%; font-weight: bold; text-align: center;">More Information</td>
			    </tr>
EOBODY;
		if($query !== false) {
		    while($row = $query->fetch_assoc()) {
			$edit_time = date("H:i:s, d M Y", $row['time']);
			
			$edit_type = ProjectEditType::resolveTypeToText($row['type']);
			
			$edit_user = ProjectEditType::resolveUser($row['uid_editor']);
			
			if($row['info1'] == null) {
			    $edit_info1 = '';
			} else {
			    if(ProjectEditType::isUserEdit($row['type']) === true) {
				$edit_user_info1 = ProjectEditType::resolveUser($row['info1']);
				if($edit_user_info1 === false) {
				    $userLink = '<i>Deleted User</i>';
				} else {
				    $userLink = '<a href="'.EclipseMDE::userLink($edit_user_info1->get_userName()).'">'.$edit_user_info1->get_userName().'</a>';
				}
				$edit_info1 = $userLink; unset($userLink);
			    } elseif(ProjectEditType::isTagEdit($row['type']) === true) {
				$edit_tag_info1 = ProjectEditType::resolveTag($row['info1']);
				if($edit_tag_info1 === false) {
				    $tagLink = '<i>Deleted User</i>';
				} else {
				    $tagLink = $edit_tag_info1->getName();
				}
				$edit_info1 = $tagLink; unset($tagLink);
			    } else {
				$edit_info1 = $row['info1'];
			    }
			}
			if($row['info2'] == null) {
			    $edit_info2 = '';
			} else {
			    $edit_info2 = '';
			    if(strlen($edit_info1) > 0) {
				$edit_info2 .= ', ';
			    }
			    $edit_info2 .= '('.$row['info2'].')';
			}
			if($edit_user === false) {
			    $userLink = '<i>Deleted User</i>';
			} else {
			    $userLink = '<a href="'.EclipseMDE::userLink($edit_user->get_userName()).'">'.$edit_user->get_userName().'</a>';
			}
			
			$table_body .= <<<EOBODY
			    <tr>
				<td style="width: 25%; text-align: center;">{$edit_time}</td>
				<td style="width: 25%; text-align: center;">{$edit_type}</td>
				<td style="width: 25%; text-align: center;">{$userLink}</td>
				<td style="width: 25%; text-align: center; font-style: italic;">{$edit_info1}{$edit_info2}</td>
			    </tr>
EOBODY;
		    }
		} else {
		    
		}
		$table_foot = <<<EOFOOT
		    </table>
EOFOOT;
		
		$TemplateEngine->create_content_html($editer_line);
		$TemplateEngine->create_content_html($table_head);
		    $TemplateEngine->create_content_html($table_body);
		$TemplateEngine->create_content_html($table_foot);
		$TemplateEngine->render();
		die();
	    } else {
		$gVAR['e'] = 403;
		require_once('error_handler.php');
		die();
	    }
	} catch(InvalidProjectException $e) {
	    
	}
    }
    
    $gVAR['e'] = 404;
    require_once('error_handler.php');
    die();
    
    
?>